package com.wang.common.exception;

import com.wang.common.enums.ExceptionEnum;
import com.wang.common.result.Result;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.UnknownHostException;

/**
 * @Author wang
 * @Date 2024/2/14 19:30
 * @PackageName:com.atguigu.system.exception
 * @ClassName: GuiguException
 * @Description: TODO
 * @Version 1.0
 */

@Data
@AllArgsConstructor //有参方法
@NoArgsConstructor // 无参方法
public class ServiceException extends RuntimeException {

    private Integer code;
    private String msg;


    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = ServiceException.class)
    @ResponseBody
    public Result ServiceExceptionHandler(HttpServletRequest req, ServiceException e){
        logger.error("发生业务异常！原因是：{}",e.getMessage());
        return Result.fail(e.getCode(),e.getMsg());
    }

    /**
     * 处理空指针的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest req, NullPointerException e){
        logger.error("发生空指针异常！原因是:",e);
        return Result.fail(ExceptionEnum.NOT_FOUND);
    }

    /**
     * 处理网络的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = UnknownHostException.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest req, UnknownHostException e){
        logger.error("发生网络异常！原因是:",e);
        return Result.fail(ExceptionEnum.UNKNOWNHOST);
    }

    /**
     * 处理连接超时的异常
     * @param req
     * @param e
     * @return
     */
//    @ExceptionHandler(value = RuntimeException.class)
//    @ResponseBody
//    public Result exceptionHandler(HttpServletRequest req, RuntimeException e){
//        logger.error("发生超时异常！原因是:",e);
//        return Result.error(ExceptionEnum.REQUEST_TIMEOUT);
//    }


    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest req, Exception e){
        System.out.println(req.getHeader("code"));

        logger.error("未知异常！原因是:",e);
        if ( e.getMessage().contains("400")){return Result.fail(ExceptionEnum.BAD_REQUEST);}
        if ( e.getMessage().contains("401")){return Result.fail(ExceptionEnum.UNAUTHORIZED);}
        if ( e.getMessage().contains("404")){return Result.fail(ExceptionEnum.NOT_FOUND);}
        if ( e.getMessage().contains("405")){return Result.fail(ExceptionEnum.Method_Not_Allowed);}
        if ( e.getMessage().contains("406")){return Result.fail(ExceptionEnum.NOT_ACCEPTABLE);}
        if ( e.getMessage().contains("409")){return Result.fail(ExceptionEnum.CONFLICT);}
        if ( e.getMessage().contains("410")){return Result.fail(ExceptionEnum.GONE);}
        if ( e.getMessage().contains("411")){return Result.fail(ExceptionEnum.LENGTH_REQUIRED);}
        if ( e.getMessage().contains("412")){return Result.fail(ExceptionEnum.PRECONDITION_FAILED);}
        if ( e.getMessage().contains("413")){return Result.fail(ExceptionEnum.REQUEST_ENTITY_TOO_LARGE);}
        if ( e.getMessage().contains("415")){return Result.fail(ExceptionEnum.UNSUPPORTED_MEDIA_TYPE);}
        if ( e.getMessage().contains("416")){return Result.fail(ExceptionEnum.REQUESTED_RANGE_NOT_SATISFIABLE);}
        if ( e.getMessage().contains("422")){return Result.fail(ExceptionEnum.UNPROCESSABLE_ENTITY);}
        if ( e.getMessage().contains("429")){return Result.fail(ExceptionEnum.TOO_MANY_REQUESTS);}
        if ( e.getMessage().contains("500")){return Result.fail(ExceptionEnum.INTERNAL_SERVER_ERROR);}
        if ( e.getMessage().contains("501")){return Result.fail(ExceptionEnum.NOT_IMPLEMENTED);}
        if ( e.getMessage().contains("507")){return Result.fail(ExceptionEnum.Insufficient_Storage);}
        return Result.fail(ExceptionEnum.INTERNAL_SERVER_ERROR);
    }

}
